package cn.wolfcode.wechat.service.impl;

import cn.wolfcode.wechat.domain.Account;
import cn.wolfcode.wechat.domain.Client;
import cn.wolfcode.wechat.domain.Recharge;
import cn.wolfcode.wechat.mapper.RechargeMapper;
import cn.wolfcode.wechat.query.RechargeQueryObject;
import cn.wolfcode.wechat.service.IAccountService;
import cn.wolfcode.wechat.service.IClientService;
import cn.wolfcode.wechat.service.IRechargeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class RechargeServiceImpl implements IRechargeService{
    @Autowired
    private RechargeMapper rechargeMapper;
    @Autowired
    private IClientService clientService;
    @Autowired
    private IAccountService accountService;

    @Override
    public int save(Recharge recharge) {
        return rechargeMapper.insert(recharge);
    }

    @Override
    public int update(Recharge recharge) {
        return rechargeMapper.updateByPrimaryKey(recharge);
    }

    @Override
    public Recharge get(Long id) {
        return rechargeMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(RechargeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List result = rechargeMapper.queryPage(qo);
        return new PageInfo(result);
    }

    @Override
    public Long getClientIdByOpenId(String openId) {
        return rechargeMapper.getClientIdByOpenId(openId);
    }

    @Override
    public void audit(Long id, int state, String remark) {
        Recharge recharge = this.get(id);
        Account account = accountService.get(recharge.getClient().getId());
        if (recharge != null && recharge.getState() == Recharge.STATE_NORMAL){
            recharge.setAuditTime(new Date());
            recharge.setRemark(remark);
            if (state == Recharge.STATE_PASS){
                recharge.setState(Recharge.STATE_PASS);
                // 设置账户可用余额
                account.setUsableAmount(account.getUsableAmount().add(recharge.getAmount()));
                accountService.update(account);
            }else {
                recharge.setState(Recharge.STATE_REJECT);
            }
            this.update(recharge);
        }
    }

    @Override
    public void rechargeSave(Recharge recharge, String openId) {
        Client client = clientService.selectByOpenid(openId);
        Recharge re = new Recharge();
        re.setAmount(recharge.getAmount());
        re.setNote(recharge.getNote());
        re.setBank(recharge.getBank());
        re.setClient(client);
        re.setTradeCode(recharge.getTradeCode());
        this.save(re);
    }

    @Override
    public Client getClientByOpenId(String openId) {
        return rechargeMapper.getClientByOpenId(openId);
    }

    @Override
    public Account getAccountByClientId(Long ClientId) {
        return rechargeMapper.getAccountByClientId(ClientId);
    }
}
